package itdep.domain.security;

import ga.security.Secured;
import ga.security.SecuredAccount;
import itdep.domain.IisEntity;
import itdep.domain.division.Employee;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Ivan Khalopik
 * @version $Revision: 31 $ $Date: 2010-06-20 07:48:02 +0000 (Sun, 20 Jun 2010) $
 */
@Entity
@Table(name = "ACCOUNT")
public class Account extends IisEntity implements SecuredAccount {
	public static final String EMAIL_PROPERTY = "email";

	@Column(name = "EMAIL", nullable = false)
	private String email;

	@Column(name = "PASSWORD", nullable = false)
	private String password;

	@Column(name = "REGISTRATION_DATE", nullable = true, updatable = false)
	private Date registered;

	@Column(name = "LAST_LOGIN_DATE", nullable = true, insertable = false)
	private Date lastLogin;

	@ManyToMany
	@JoinTable(name = "ACCOUNT_ROLE",
			joinColumns = @JoinColumn(name = "ID_ACCOUNT", nullable = false),
			inverseJoinColumns = @JoinColumn(name = "ID_ROLE", nullable = false))
	private Set<Role> roles = new HashSet<Role>();

	@Transient
	private Employee employee;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getRegistered() {
		return registered;
	}

	public void setRegistered(Date registered) {
		this.registered = registered;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public boolean isEnabled() {
		return true;
	}

	public boolean isExpired() {
		return false;
	}

	public boolean isLocked() {
		return false;
	}

	public boolean isPasswordExpired() {
		return false;
	}

	public String getAuthority() {
		return null;
	}

	public Collection<? extends Secured> getChildrenSecured() {
		return getRoles();
	}

}